<template>
  <div class="network-management">
    <el-card class="management-card" shadow="hover">
      <div slot="header" class="card-header">
        <div class="header-left">
          <i class="el-icon-connection"></i>
          <span>安全域名</span>
          <el-tooltip content="管理域名解析和海外加速服务" placement="right">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
        <div class="header-right">
          <el-button-group>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-plus"
              @click="showPurchaseDialog">
              新购
            </el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-upload2"
              @click="showOverseasDialog">
              海外加速域名
            </el-button>
          </el-button-group>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="请输入域名、IP、描述搜索"
          prefix-icon="el-icon-search"
          clearable
          @clear="handleSearch"
          @keyup.enter.native="handleSearch"
          style="width: 300px">
        </el-input>
      </div>

      <!-- 域名列表 -->
      <el-table
        :data="filteredTableData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        stripe>
        <el-table-column prop="customerName" label="客户名称" min-width="200"></el-table-column>
        <el-table-column prop="host" label="所属主机" min-width="150">
          <template slot-scope="scope">
            <span class="host-ip">{{ scope.row.host }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="domain" label="业务域名" min-width="200">
          <template slot-scope="scope">
            <span class="domain-name">{{ scope.row.domain }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === '解析正常' ? 'success' : 'danger'"
              size="small">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="180"></el-table-column>
        <el-table-column prop="description" label="描述" min-width="150"></el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="pagination-container">
        <span class="total-count">共 {{ total }} 条数据</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 购买新机对话框 -->
    <el-dialog
      title="购买新机"
      :visible.sync="purchaseDialogVisible"
      width="500px"
      :close-on-click-modal="false">
      <el-form :model="purchaseForm" ref="purchaseForm" :rules="purchaseRules" label-width="80px">
        <el-form-item label="企业" prop="enterprise">
          <el-select
            v-model="purchaseForm.enterprise"
            placeholder="请选择企业"
            style="width: 100%">
            <el-option label="畅捷通信息技术股份有限公司" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置" prop="configuration">
          <el-select
            v-model="purchaseForm.configuration"
            placeholder="请选择要购买商品"
            style="width: 100%">
            <el-option label="基础配置" value="basic"></el-option>
            <el-option label="高级配置" value="advanced"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="purchaseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlePurchaseSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 海外加速域名对话框 -->
    <el-dialog
      title="海外加速域名"
      :visible.sync="overseasDialogVisible"
      width="500px"
      :close-on-click-modal="false">
      <el-form :model="overseasForm" ref="overseasForm" :rules="overseasRules" label-width="100px">
        <el-form-item label="下单企业" prop="enterprise" required>
          <el-select
            v-model="overseasForm.enterprise"
            placeholder="请选择需要下单的企业"
            style="width: 100%">
            <el-option label="畅捷通信息技术股份有限公司" value="1"></el-option>
          </el-select>
        </el-form-item>
        <div class="overseas-notice">
          若有在海外使用的场景，可购买海外加速域名，购买后联系畅云管家工作人员开通。
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="overseasDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOverseasSubmit">去购买</el-button>
      </div>
    </el-dialog>

    <!-- 修改域名对话框 -->
    <el-dialog
      title="修改域名"
      :visible.sync="editDialogVisible"
      width="500px"
      :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules" label-width="80px">
        <el-form-item label="域名" prop="domain" required>
          <el-input v-model="editForm.domainPrefix" style="width: 40%" placeholder="请输入域名前缀">
          </el-input>
          <span style="margin: 0 5px;">.</span>
          <el-select v-model="editForm.domainSuffix" style="width: 55%">
            <el-option label="a4.qjtmsp.com" value="a4.qjtmsp.com"></el-option>
          </el-select>
        </el-form-item>
        <div class="edit-warning">
          <i class="el-icon-warning"></i>
          仅支持修改一次，修改后不支持再次修改！
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'NetworkManagement',
  data() {
    return {
      // 搜索相关
      searchQuery: '',
      loading: false,

      // 表格数据
      tableData: [
        {
          customerName: '畅捷通信息技术股份有限公司',
          host: '121.89.206.20',
          domain: '206020.a4.qjtmsp.com',
          status: '解析正常',
          createTime: '2024-03-11 15:45:36',
          description: '畅捷通公司'
        }
      ],

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 1,

      // 购买新机表单
      purchaseDialogVisible: false,
      purchaseForm: {
        enterprise: '',
        configuration: ''
      },
      purchaseRules: {
        enterprise: [{ required: true, message: '请选择企业', trigger: 'change' }],
        configuration: [{ required: true, message: '请选择配置', trigger: 'change' }]
      },

      // 海外加速域名表单
      overseasDialogVisible: false,
      overseasForm: {
        enterprise: ''
      },
      overseasRules: {
        enterprise: [{ required: true, message: '请选择下单企业', trigger: 'change' }]
      },

      // 修改域名表单
      editDialogVisible: false,
      editForm: {
        domainPrefix: '',
        domainSuffix: 'a4.qjtmsp.com'
      },
      editRules: {
        domainPrefix: [{ required: true, message: '请输入域名前缀', trigger: 'blur' }]
      }
    }
  },
  computed: {
    filteredTableData() {
      if (!this.searchQuery) {
        return this.tableData
      }
      const query = this.searchQuery.toLowerCase()
      return this.tableData.filter(data => {
        return data.domain.toLowerCase().includes(query) ||
          data.host.includes(query) ||
          data.description.toLowerCase().includes(query)
      })
    }
  },
  methods: {
    // 搜索相关方法
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },

    // 分页相关方法
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 对话框相关方法
    showPurchaseDialog() {
      this.purchaseDialogVisible = true
    },
    showOverseasDialog() {
      this.overseasDialogVisible = true
    },
    handleEdit(row) {
      this.editForm.domainPrefix = row.domain.split('.')[0]
      this.editDialogVisible = true
    },

    // 表单提交方法
    handlePurchaseSubmit() {
      this.$refs.purchaseForm.validate(valid => {
        if (valid) {
          this.$message.success('购买成功')
          this.purchaseDialogVisible = false
        }
      })
    },
    handleOverseasSubmit() {
      this.$refs.overseasForm.validate(valid => {
        if (valid) {
          this.$message.success('提交成功')
          this.overseasDialogVisible = false
        }
      })
    },
    handleEditSubmit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$message.success('域名修改成功')
          this.editDialogVisible = false
        }
      })
    },

    // 获取列表数据
    getList() {
      this.loading = true
      // 模拟API调用
      setTimeout(() => {
        this.loading = false
      }, 800)
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.network-management {
  padding: 10px;
}

.management-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-left i {
  font-size: 18px;
}

.header-left .el-icon-question {
  font-size: 14px;
  color: #909399;
  cursor: help;
}

.search-bar {
  margin-bottom: 20px;
}

.host-ip {
  font-family: monospace;
  color: #409EFF;
}

.domain-name {
  font-family: monospace;
  color: #67C23A;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.total-count {
  color: #606266;
  font-size: 14px;
}

/* 对话框样式 */
.overseas-notice {
  margin: 20px 0;
  padding: 12px;
  background-color: #f4f4f5;
  border-radius: 4px;
  color: #909399;
  font-size: 14px;
  line-height: 1.4;
}

.edit-warning {
  margin: 15px 0;
  color: #E6A23C;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.edit-warning i {
  font-size: 16px;
}

/* 表格样式优化 */
.el-table {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.el-table th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 表单样式优化 */
.el-select {
  width: 100%;
}

/* 标签样式 */
.el-tag {
  border-radius: 4px;
  padding: 0 8px;
  height: 24px;
  line-height: 22px;
}

/* 加载动画样式 */
.el-loading-spinner .el-icon-loading {
  font-size: 30px;
  color: #409EFF;
}

.el-loading-spinner .el-loading-text {
  color: #409EFF;
  margin: 3px 0;
  font-size: 14px;
}
</style>
